//
//  AppDelegate.swift
//  仿微博
//
//  Created by apple on 2016/12/9.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //应用程序额外信息
        setupAdditions()
            window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
      loadInfo()
        return true
    }

   

}


// MARK: - 设置应用程序额外信息
extension AppDelegate {
    fileprivate func setupAdditions() {
        // 1.设置 SVProgressHUD 最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        //2.设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        //3.设置用户授权显示通知
        //  ios  10
        if #available(iOS 10.0, *){
            
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound], completionHandler: { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            })
        }else{
            // ios 10 以下
            //   取得用户授权显示通知【上方的提示条/声音/badgeNumber】
            let notifySetting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySetting)
            
            
        }
      
    }
    


}
// MARK: - 从服务器加载应用程序信息
extension AppDelegate {
    fileprivate func loadInfo() {
    //1.模拟异步
        DispatchQueue.global().async {
            //1.URL
//
            let url = Bundle.main.url(forResource: "man.json", withExtension: nil)
            //2.data
            let data = NSData(contentsOf: url!)
            //3.写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("man.json")
            //直到保存到沙盒，等待下一次程序启动使用
            data?.write(toFile: jsonPath, atomically: true)
           // print("应用程序加载完毕\(jsonPath)")
            
        }
    
    
    }
    
    
    
    
    
    
    
    
}
